<?php
// +----------------------------------------------------------------------
// | Description: 上传图片类
// +----------------------------------------------------------------------
// | version 1.0
// +----------------------------------------------------------------------
// | Author: xiaoliu <528772568@qq.com>
// +----------------------------------------------------------------------

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\api\validate\UploadValidate;
use think\Image;
use think\Request;

class Upload extends Api
{

    protected $noNeedLogin = ['index'];
    protected $noNeedRight = '*';
    protected function _initialize()
    {
        parent::_initialize();
    }
    /**
     * [index 上传]
     * @param   $[fileName] [<retain:原图,file:压缩图片>]
     * @return [json] [description]
     */
    public function index()
    {
        $msg = '上传失败!';
        $arr = [];
        $request = Request::instance();
        if ($request->isPost()) {
            $fileName = key($_FILES);
            $file = $request->file($fileName);
            if(!$file){
                $this->error('请上传文件!');
            }
            $upload = $file->getInfo();
            $imgValidate = new UploadValidate();
            $imgVerify = $imgValidate->scene('fileMax')->check(['fileMax' => $file]);
            if ($imgVerify) {
                // 保存路径
                $date = date('Ymd');
                $path =  ROOT_PATH . 'public' . DS . 'uploads'. DS . $date;
                $info = $file->move($path,md5_file($upload['tmp_name']));
                if($info){
                    $fileName =  $info->getSaveName();
                    $url =  '/uploads/'.$date.'/'.$fileName;
                    // $arr=explode(".", $url);
                    // if(end($arr)=='mp4'){

                    // }
                    $this->success('上传成功!',$url);
                    print_r($arr);
                }
               
            } else {
                $msg = $imgValidate->getError();
            }
        }
        $this->error($msg, $arr);
    }
}
